package dto

import (
	"github.com/open-snail/snail-job-go/util"
	"github.com/sirupsen/logrus"
)

type Options struct {
	ServerHost    string           `description:"服务器IP地址（或域名）；集群时建议通过 nginx 做负载均衡"`
	ServerPort    string           `description:"服务器通讯端口（不是后台管理页面服务端口）"`
	HostIP        string           `description:"客户端绑定IP，必须服务器可以访问到；默认自动推断，在服务器无法调度客户端时需要手动配置"`
	HostPort      string           `description:"客户端通讯端口，默认 17889"`
	Namespace     string           `description:"命名空间"`
	GroupName     string           `description:"接入组名"`
	Token         string           `description:"接入组 token"`
	SystemVersion string           `description:"系统版本"`
	ExecutorType  string           `description:"执行器类型"`
	Labels        string           `description:"标签"`
	Level         logrus.Level     `description:"输出日志的等级"`
	ReportCaller  bool             `description:"日志是否显示调度者的信息"`
	Formatter     logrus.Formatter `description:"日志格式"`
}

func NewClientOptions(opts ...OptionFunc) *Options {
	clientOptions := &Options{
		ServerHost:    "127.0.0.1",
		ServerPort:    "17888",
		HostIP:        "127.0.0.1",
		HostPort:      "17889",
		Namespace:     "764d604ec6fc45f68cd92514c40e9e1a",
		GroupName:     "snail_job_demo_group",
		Token:         "SJ_Wyz3dmsdbDOkDujOTSSoBjGQP1BMsVnj",
		SystemVersion: "0.0.1",
		ExecutorType:  "3", // go
		Level:         logrus.InfoLevel,
		ReportCaller:  true,
	}

	for _, opt := range opts {
		opt(clientOptions)
	}

	return clientOptions
}

type OptionFunc func(*Options)

// WithServerHost ...
func WithServerHost(serverHost string) OptionFunc {
	return func(config *Options) {
		config.ServerHost = serverHost
	}
}

// WithServerPort ...
func WithServerPort(serverPort string) OptionFunc {
	return func(config *Options) {
		config.ServerPort = serverPort
	}
}

// WithHostIP ...
func WithHostIP(hostIP string) OptionFunc {
	return func(config *Options) {
		config.HostIP = hostIP
	}
}

// WithHostPort ...
func WithHostPort(hostPort string) OptionFunc {
	return func(config *Options) {
		config.HostPort = hostPort
	}
}

// WithNamespace ...
func WithNamespace(namespace string) OptionFunc {
	return func(config *Options) {
		config.Namespace = namespace
	}
}

// WithGroupName ...
func WithGroupName(groupName string) OptionFunc {
	return func(config *Options) {
		config.GroupName = groupName
	}
}

// WithToken ...
func WithToken(token string) OptionFunc {
	return func(config *Options) {
		config.Token = token
	}
}

// WithLogLevel ...
func WithLogLevel(level logrus.Level) OptionFunc {
	return func(config *Options) {
		config.Level = level
	}
}

// WithReportCaller ...
func WithReportCaller(enabled bool) OptionFunc {
	return func(config *Options) {
		config.ReportCaller = enabled
	}
}

// WithFormatter ...
func WithFormatter(formatter logrus.Formatter) OptionFunc {
	return func(config *Options) {
		config.Formatter = formatter
	}
}

// WithLabels ...
func WithLabels(labels string) OptionFunc {
	return func(config *Options) {
		config.Labels = util.TransformLabels(labels)
	}
}
